# 1.创建会话
"""

"""
from sqlalchemy.orm import sessionmaker
from create_table_foreignkey import engine
Session = sessionmaker(engine)
db_session = Session()

# 2.Girl Boy
from create_table_manytomany import Girls, Boys
"""
添加
"""
# 反向添加  通过boy添加girl
boy_obj = Boys(name="Jack")
boy_obj.boy2girl = [Girls(name="Anny"),Girls(name="Lux")]
db_session.add(boy_obj)
db_session.commit()
db_session.close()


# 正向添加  通过girl添加boy
girl_obj = Girls(name="Kaselin",girl2boy=[Boys(name="Yase"),Boys(name="Ezreal")])
db_session.add(girl_obj)
db_session.commit()
db_session.close()


"""
查询
"""
# 正向查询
# girl_list = db_session.query(Girls).all()
# for girl in girl_list:
#     for boy in girl.girl2boy:
#         print(girl.name,boy.name)

# 反向查询
boy_list = db_session.query(Boys).all()

for boy in boy_list:
    for girl in boy.boy2girl:
        print(boy.name, girl.name)

db_session.close()